<?php
include './SqlBuilder.php';

$builder = new SqlBuilder();

// 查询示例
$sql = $builder->table('users')
    ->fields(['id', 'name', 'age'])
    ->where('age', 18, '>')
    ->where('status', [1, 2])
    ->orderBy('id', 'DESC')
    ->limit(10)
    ->get();
echo "查询示例：\n";
echo $sql;
echo PHP_EOL;
// 插入示例
$sql = $builder->table('users')
    ->insert([
        'name' => 'test',
        'age' => 20,
        'created_at' => time()
    ]);
echo "插入示例：\n";
echo $sql;
echo PHP_EOL;
// 更新示例
$sql = $builder->table('users')
    ->where('id', 1)
    ->update([
        'name' => 'new name',
        'age' => 21
    ]);
echo "更新示例：\n";
echo $sql;
echo PHP_EOL;
// 删除示例
$sql = $builder->table('users')
    ->where('id', 1)
    ->delete();
echo "删除示例：\n";
echo $sql;
echo PHP_EOL;

$sql = $builder->table('users', 'u')
    ->fields(['u.id', 'u.name'])
    ->join('orders', 'users.id = orders.user_id', "LEFT", "o")
    ->where([
        'u.status' => 1,
        'u.age' => [18, 20, 25]
    ])
    ->orWhere('u.vip', 1)
    ->groupBy('u.department')
    ->having('count(*) > 5')
    ->orderBy('u.id', 'DESC')
    ->limit(10)
    ->get();
echo "查询示例2：\n";
echo $sql;
echo PHP_EOL;


// 示例1：插入多条用户数据
$users = [
    [
        'name' => '张三',
        'age' => 20,
        'gender' => 1,
        'created_at' => time()
    ],
    [
        'name' => '李四',
        'age' => 25,
        'gender' => 1,
        'created_at' => time()
    ],
    [
        'name' => '王五',
        'age' => 30,
        'gender' => 2,
        'created_at' => time()
    ]
];

$sql = $builder->table('users')->insertBatch($users);
echo "示例1：\n";
echo $sql;
echo PHP_EOL;
// 生成的SQL：
// INSERT INTO `users` (`name`, `age`, `gender`, `created_at`)
// VALUES
// ('张三', 20, 1, 1703491234),
// ('李四', 25, 1, 1703491234),
// ('王五', 30, 2, 1703491234)

// 示例2：插入订单数据
$orders = [
    [
        'order_no' => 'ORD001',
        'user_id' => 1,
        'amount' => 99.99,
        'status' => 1
    ],
    [
        'order_no' => 'ORD002',
        'user_id' => 2,
        'amount' => 199.99,
        'status' => 1
    ]
];

$sql = $builder->table('orders')->insertBatch($orders);

echo "示例2：\n";
echo $sql;
echo PHP_EOL;


function cpuAction()
{
    $startTime = microtime(true);

    // 模拟大量计算
    $result = 0;
    for ($i = 0; $i < 100000; $i++) {
        $result += sqrt($i) * sin($i);
        var_dump( ' cpu calculate progress: ' . $i . '/100000');
        // 每隔一定次数输出进度
        if ($i % 1000 === 0) {
            $progress = ($i / 10000) * 100;
            var_dump( " 计算进度: " . number_format($progress, 2) . "%");
        }
    }

    $endTime = microtime(true);
    $timeUsed = round($endTime - $startTime, 4);

    $response = [
        'result' => $result,
        'time_used' => $timeUsed . 's'
    ];
    var_dump( $response );
}

cpuAction();